package com.example.administrator.xtu_community.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import com.example.administrator.xtu_community.Adapter.NewsRecyclerViewAdapter;
import com.example.administrator.xtu_community.R;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.touch.OnItemMoveListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.example.administrator.xtu_community.simpleClass.News;

/**
 * NewsFragment
 * 用于发布实验室的新闻
 * 如比赛通知
 */
public class NewsFragment extends Fragment {

    private static final String TAG = "NewsFragment";
    private SwipeRefreshLayout swipeRefresh;
    private SwipeRecyclerView swipeRecyclerView;
    private final News[] news = {
            new News("2021-11-12","Apple", "AppleAppleAppleAppleAppleAppleAppleAppleAppleAppleApple" +
                    "AppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleApple" +
                    "AppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleApple" +
                    "AppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleApple" +
                    "AppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleAppleApple",
                    R.drawable.boy, "裴教授"),
            new News("2021-11-12","Orange", "Orange", R.drawable.boy, "裴教授"),
            new News("2021-11-12","Banana", "Orange", R.drawable.boy, "裴教授"),
            new News("2021-11-12","Watermelon", "Orange", R.drawable.girl, "曾老师"),
            new News("2021-11-12","Cherry", "Orange", R.drawable.girl, "曾老师"),
            new News("2021-11-12","Mango", "Orange", R.drawable.boy, "裴教授"),
            new News("2021-11-12","Pear", "Orange", R.drawable.girl, "曾老师"),
            new News("2021-11-12","Grape", "Orange", R.drawable.boy, "裴教授"),
            new News("2021-11-12","Strawberry", "Orange", R.drawable.girl, "曾老师")
    };


    private final List<News> newsList = new ArrayList<>();

    public NewsFragment() {

    }

    public static NewsFragment newInstance() {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        initNews();

        //使用开源库SwipeRecyclerView做侧滑删除
        swipeRecyclerView = view.findViewById(R.id.news_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        swipeRecyclerView.setLayoutManager(layoutManager);
        NewsRecyclerViewAdapter adapter = new NewsRecyclerViewAdapter(newsList);
        // 设置监听器。
        swipeRecyclerView.setSwipeMenuCreator(new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getContext()); // 各种文字和图标属性设置。
                leftMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。
            }
        });
        // 菜单点击监听。
        swipeRecyclerView.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                // 菜单在Item中的Position：
                int menuPosition = menuBridge.getPosition();
            }
        });
        // 打开拖拽排序
        swipeRecyclerView.setLongPressDragEnabled(true);
        // 打开侧滑删除
        swipeRecyclerView.setItemViewSwipeEnabled(true);
        // 监听拖拽，更新UI
        swipeRecyclerView.setOnItemMoveListener(new OnItemMoveListener() {
            @Override
            public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
                // 此方法在Item拖拽交换位置时被调用。
                // 第一个参数是要交换为之的Item，第二个是目标位置的Item。

                // 交换数据，并更新adapter。
                int fromPosition = srcHolder.getAdapterPosition();
                int toPosition = targetHolder.getAdapterPosition();
                Collections.swap(newsList, fromPosition, toPosition);
                adapter.notifyItemMoved(fromPosition, toPosition);

                // 返回true，表示数据交换成功，ItemView可以交换位置。
                return true;
            }

            @Override
            public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {
                // 此方法在Item在侧滑删除时被调用。

                // 从数据源移除该Item对应的数据，并刷新Adapter。
                int position = srcHolder.getAdapterPosition();
                newsList.remove(position);
                adapter.notifyItemRemoved(position);
            }
        });
        swipeRecyclerView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            Log.d(TAG, "setUserVisibleHint: ");
            swipeRecyclerView.getAdapter().notifyDataSetChanged();
            swipeRecyclerView.scheduleLayoutAnimation();
        }

    }
    private void refreshFruits() {
    }

    private void initNews() {
        newsList.addAll(Arrays.asList(news));
    }
}